package com.fire.communication.web.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.fire.communication.common.base.BaseService;
import com.fire.communication.model.DeviceBusiness;
import com.fire.communication.model.margin.SmsVoiceMargin;
import com.fire.communication.support.model.RespEntity;
import com.fire.communication.support.mybatisplus.QueryWrapper;
import com.fire.communication.support.utils.requestutil.HttpRequestUtil;
import com.fire.communication.web.mapper.margin.SmsVoiceMarginMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author: YangYu
 * @ClassName: SmsVoiceMarginService
 * @Date: Created in 13:56 2023/5/26
 * @Description:
 */
@Service
public class SmsVoiceMarginService extends BaseService {

    @Resource
    private SmsVoiceMarginMapper smsVoiceMarginMapper;

    /**
     * @Author YangYu
     * @Description 余量新增、编辑
     * @Date Created in 14:02 2023/5/26
     * @Param [jsonParam]
     */

    public RespEntity addOrUpdate(JSONObject jsonParam){

        String id = "";
        if (jsonParam.containsKey("id")){
            id = jsonParam.getStr("id");
            //计算分配余量
            //SmsVoiceMargin smsVoiceMarginP = smsVoiceMarginMapper.selectOne(new QueryWrapper<SmsVoiceMargin>().eq("home_id",jsonParam.getStr("pid")).eq("type",jsonParam.getInt("type")));
            SmsVoiceMargin smsVoiceMargin = smsVoiceMarginMapper.selectById(id);
            if (smsVoiceMargin == null){
                return RespEntity.fail("无效ID");
            }
//            if (smsVoiceMargin.getCount() > jsonParam.getInt("count")){
//                int count = smsVoiceMargin.getCount() - jsonParam.getInt("count");
//                smsVoiceMarginP.setCount(smsVoiceMarginP.getCount() + count);
//            }else if (smsVoiceMargin.getCount() < jsonParam.getInt("count")){
//                int count =  jsonParam.getInt("count") - smsVoiceMargin.getCount();
//                if (count > smsVoiceMarginP.getCount()){
//                    return RespEntity.fail("无法分配条数，已超出总条数。");
//                }else {
//                    smsVoiceMarginP.setCount(smsVoiceMarginP.getCount() - count);
//                }
//            }
            //更新上级余量
            //smsVoiceMarginMapper.updateById(smsVoiceMarginP);
            //删除
            smsVoiceMarginMapper.deleteById(id);
        }else {
            id = UUID.randomUUID().toString();
            int count = smsVoiceMarginMapper.selectCount(new QueryWrapper<SmsVoiceMargin>().eq("home_id",jsonParam.getStr("homeId")).eq("type",jsonParam.getInt("type")));
            if (count > 0){
                return RespEntity.fail("已设置条数，无需新增，请修改。");
            }
//            //计算余量、更新上级余量
//            SmsVoiceMargin smsVoiceMarginP = smsVoiceMarginMapper.selectOne(new QueryWrapper<SmsVoiceMargin>().eq("home_id",jsonParam.getStr("pid")).eq("type",jsonParam.getInt("type")));
//            if (jsonParam.getInt("count") > smsVoiceMarginP.getCount()){
//                return RespEntity.fail("无法分配条数，已超出总条数。");
//            }
//            smsVoiceMarginP.setCount(smsVoiceMarginP.getCount() - jsonParam.getInt("count"));
//            smsVoiceMarginMapper.updateById(smsVoiceMarginP);
        }
        SmsVoiceMargin smsVoiceMargin = new SmsVoiceMargin();
        smsVoiceMargin.setId(id);
        smsVoiceMargin.setHomeId(jsonParam.getStr("homeId"));
        smsVoiceMargin.setCount(jsonParam.getInt("count"));
        smsVoiceMargin.setType(jsonParam.getInt("type"));
        smsVoiceMargin.setAllCount(jsonParam.getInt("count"));
        if (jsonParam.containsKey("orgId")){
            smsVoiceMargin.setOrgId(jsonParam.getStr("orgId"));
        }
        smsVoiceMarginMapper.insert(smsVoiceMargin);
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 短信、语音余量列表
     * @Date Created in 14:24 2023/5/26
     * @Param [jsonParam]
     */

    public RespEntity smsVoiceMarginList(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        List<SmsVoiceMargin> smsVoiceMarginList = smsVoiceMarginMapper.selectList(new QueryWrapper<SmsVoiceMargin>().eq("home_id",jsonParam.getStr("homeId")));
        return RespEntity.success(smsVoiceMarginList);
    }

    /**
     * @Author YangYu
     * @Description 批量插入单位短信、语音使用条数
     * @Date Created in 16:29 2023/5/26
     * @Param [jsonParam]
     */

    public RespEntity setBusinessCount(JSONObject jsonParam){
        List<String> businessIdList = smsVoiceMarginMapper.businessIdList(jsonParam.getStr("homeId"));
        if (businessIdList.size() == 0){
            return RespEntity.success();
        }
        SmsVoiceMargin smsVoiceMarginP = smsVoiceMarginMapper.selectOne(new QueryWrapper<SmsVoiceMargin>().eq("home_id",jsonParam.getStr("homeId")).eq("type",jsonParam.getInt("type")));
        if (businessIdList.size() * jsonParam.getInt("count") > smsVoiceMarginP.getCount()){
            return RespEntity.fail("无法分配条数，已超出总条数。");
        }
        List<SmsVoiceMargin> smsVoiceMarginList = new ArrayList<>();
        for (String id : businessIdList){
            SmsVoiceMargin smsVoiceMargin = new SmsVoiceMargin();
            smsVoiceMargin.setId(UUID.randomUUID().toString());
            smsVoiceMargin.setHomeId(id);
            smsVoiceMargin.setCount(jsonParam.getInt("count"));
            smsVoiceMargin.setAllCount(jsonParam.getInt("count"));
            smsVoiceMargin.setOrgId(jsonParam.getStr("homeId"));
            smsVoiceMargin.setType(jsonParam.getInt("type"));
            smsVoiceMarginList.add(smsVoiceMargin);
        }
        //删除原有的数据
        smsVoiceMarginMapper.delete(new QueryWrapper<SmsVoiceMargin>().eq("org_id",jsonParam.getStr("homeId")));
        int count = businessIdList.size() * jsonParam.getInt("count");
        smsVoiceMarginP.setCount(smsVoiceMarginP.getCount() - count);
        smsVoiceMarginP.setBusinessCount(jsonParam.getInt("count"));
        //更新上级分配条数
        smsVoiceMarginMapper.updateById(smsVoiceMarginP);
        //批量插入单位条数
        smsVoiceMarginMapper.insertZB(smsVoiceMarginList);
        return RespEntity.success();
    }


    /**
     * @Author YangYu
     * @Description 校验是否还有发送短信、语音的条数
     * @Date Created in 16:37 2023/5/26
     * @Param [businessId, type]
     */

    public boolean getTrueOrFalse(DeviceBusiness deviceBusiness,int type){
        SmsVoiceMargin smsVoiceMarginDev = smsVoiceMarginMapper.selectOne(new QueryWrapper<SmsVoiceMargin>().eq("home_id",deviceBusiness.getId()).eq("type",type));
        String[] allId = deviceBusiness.getAllId().split(",");
        String orgId = "";
        if (allId.length == 1){
            orgId = allId[0];
        }else {
            orgId = allId[1];
        }
        SmsVoiceMargin smsVoiceMarginOrg = smsVoiceMarginMapper.selectOne(new QueryWrapper<SmsVoiceMargin>().eq("home_id",orgId).eq("type",type));
        if (smsVoiceMarginDev == null || smsVoiceMarginOrg == null){
            return false;
        }else if (smsVoiceMarginOrg.getNumberPieces() >= smsVoiceMarginOrg.getCount()){
            return false;
        }else if (smsVoiceMarginDev.getNumberPieces() >= smsVoiceMarginDev.getCount()){
            return false;
        }else {
            //更新机构条数
            smsVoiceMarginOrg.setNumberPieces(smsVoiceMarginOrg.getNumberPieces() + 1);
            smsVoiceMarginMapper.updateById(smsVoiceMarginOrg);
            return true;
        }
    }

    /**
     * @Author YangYu
     * @Description 删除之后释放条数
     * @Date Created in 10:08 2023/5/27
     * @Param [jsonParam]
     */

    public RespEntity releaseNumberPieces(JSONObject jsonParam){
        if (StrUtil.isBlank(jsonParam.getStr("homeId"))) {
            return RespEntity.fail("homeId不能为空");
        }

//        List<SmsVoiceMargin> smsVoiceMarginList = smsVoiceMarginMapper.selectList(new QueryWrapper<SmsVoiceMargin>().eq("home_id",jsonParam.getStr("homeId")));
//        if (smsVoiceMarginList.size() == 0){
//            return RespEntity.success();
//        }
//        List<SmsVoiceMargin> smsVoiceMarginListP = smsVoiceMarginMapper.selectList(new QueryWrapper<SmsVoiceMargin>().eq("home_id",jsonParam.getStr("pid")));
//        for (SmsVoiceMargin smsVoiceMargin : smsVoiceMarginList){
//            for (SmsVoiceMargin smsVoiceMarginP : smsVoiceMarginListP){
//                if (smsVoiceMargin.getType().equals(smsVoiceMarginP.getType())){
//                    smsVoiceMarginP.setCount(smsVoiceMarginP.getCount() + smsVoiceMargin.getCount());
//                    smsVoiceMarginMapper.updateById(smsVoiceMarginP);
//                }
//            }
//        }
        smsVoiceMarginMapper.delete(new QueryWrapper<SmsVoiceMargin>().eq("home_id",jsonParam.getStr("homeId")));
        return RespEntity.success();
    }
}
